package admin

import (
	"config-client-v1/model"
	"config-client-v1/utils/tools"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"gorm.io/gorm"
	"strconv"
)

type PageGroupController struct {
}

func (con PageGroupController) List(ctx *gin.Context) {
	userToken := GetUserToken(ctx)
	param := struct {
		GroupName string `json:"group_name"`
		model.QueryData
	}{}
	err := ctx.ShouldBindBodyWith(&param, binding.JSON)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数",
			"data":    err.Error(),
		})
		return
	}

	tx := model.DB.Where("del = 0")
	tx.Where("project_id = ?", userToken.ProjectID)
	if param.GroupName != "" {
		tx.Where("group_name like ?", "%"+param.GroupName+"%")
	}
	var count int64
	list := make([]model.PageGroup, 0)
	err = tx.Model(&model.PageGroup{}).Count(&count).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}

	err = tx.Preload("PageList", func(db *gorm.DB) *gorm.DB {
		return db.Where("del = 0").Order("id DESC")
	}).Order("id desc").Offset(param.StartPage()).Limit(param.PageSize).Find(&list).Error

	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data": map[string]interface{}{
			"list":  list,
			"count": count,
		},
	})
}

func (con PageGroupController) Save(ctx *gin.Context) {
	userToken := GetUserToken(ctx)
	param := struct {
		Id        int    `json:"id"`
		GroupName string `json:"group_name" binding:"required,max=30"`
	}{}
	err := ctx.ShouldBindBodyWith(&param, binding.JSON)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数",
			"data":    err.Error(),
		})
		return
	}

	data := model.PageGroup{}
	data.ProjectID = userToken.ProjectID
	data.GroupName = param.GroupName
	now := tools.GetNow()
	if param.Id == 0 {
		data.CreateTime = &now
		data.UpdateTime = &now
		err = model.DB.Omit("PageList").Create(&data).Error
	} else {
		group := &model.PageGroup{}
		groupExist, err := group.Exists(param.Id)
		if !groupExist {
			ctx.JSON(200, gin.H{
				"code":    2,
				"message": "无效的id",
				"data":    err,
			})
			return
		}

		data.UpdateTime = &now
		err = model.DB.Model(&model.PageGroup{}).Where("id = ?", param.Id).Select("project_id", "group_name", "update_time").Updates(&data).Error
	}

	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    map[string]interface{}{},
	})
}

func (con PageGroupController) Detail(ctx *gin.Context) {
	param := struct {
		Id int `json:"id" binding:"required"`
	}{}
	if ctx.Request.Method == "POST" {
		err := ctx.ShouldBindBodyWith(&param, binding.JSON)
		if err != nil {
			ctx.JSON(200, gin.H{
				"code":    2,
				"message": "无效的参数",
				"data":    err.Error(),
			})
			return
		}
	} else {
		queryID := ctx.Query("id")
		param.Id, _ = strconv.Atoi(queryID)
	}

	info := model.PageGroup{}

	err := model.DB.Preload("PageList", func(db *gorm.DB) *gorm.DB {
		return db.Where("del = 0").Order("id DESC")
	}).Model(&model.PageGroup{}).Where("id = ?", param.Id).First(&info).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data": map[string]interface{}{
			"info": info,
		},
	})
}

func (con PageGroupController) Delete(ctx *gin.Context) {
	param := struct {
		Id int `json:"id" binding:"required"`
	}{}
	err := ctx.ShouldBindBodyWith(&param, binding.JSON)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数",
			"data":    err.Error(),
		})
		return
	}
	info := model.PageGroup{}
	err = model.DB.Model(&model.PageGroup{}).Where("id = ?", param.Id).First(&info).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}
	err = model.DB.Where("id = ?", param.Id).Delete(&model.PageGroup{}).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}
	err = model.DB.Where("group_id = ?", param.Id).Delete(&model.Page{}).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    map[string]interface{}{},
	})
}
